package org.action.inventory.controller;

import org.action.inventory.domain.entity.ActionTicket;
import org.action.inventory.service.IActionTicketService;
import org.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/actionInventory")
public class ActionInventory {
    @Autowired
    private IActionTicketService iActionTicketService;

    @PostMapping("/{id}")
    public Result deductActionTicket(@PathVariable("id") Integer id)
    {
        System.out.println(id);
        boolean result = iActionTicketService.deductActionTicket(id);
        if(result){
            System.out.println("扣减成功");
            //throw new RuntimeException("测试事务");
            return Result.success("扣减成功");
        }else{
            System.out.println("扣减失败");
            return Result.error("扣减失败");
        }
    }

    //根据ids查询活动票的价格
    @GetMapping("/getActionTicketPriceByIds")
    public Result getActionTicketPriceByIds(@RequestParam("ids") List<Integer> ids){
        return Result.success(iActionTicketService.getActionTicketPriceByIds(ids));
    }

    //根据票的id获取actionId
    @GetMapping("/getActionIdById/{id}")
    public Result getActionIdById(@PathVariable("id") Integer id){
        return Result.success(iActionTicketService.getById(id).getActionId());
    }

    //进行库存恢复
    @PostMapping("/restoreStock")
    public Result restoreStock(@RequestParam("ticketId") Integer ticketId){
        System.out.println("开始进行库存恢复");
        boolean result = iActionTicketService.restoreStock(ticketId);
        if(!result) return Result.error("库存恢复失败");
        return Result.success("库存恢复成功");
    }

    //添加活动票数信息
    @PostMapping("/addActionTicket")
    public Result addActionTicket(@RequestBody ActionTicket actionTicket) {
        System.out.println(actionTicket);
        iActionTicketService.saveActionTicket(actionTicket);
        return  Result.success("添加成功");
    }
}
